package utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import org.apache.commons.lang.StringUtils;

import com.nccsjz.utils.CardException;

/**
 * FileUtils工具类包含了操作文件的一系列方法
 * 
 * @author jason 2009-10-15
 * @since 1.0
 */
public class FileUtils {

	/**
	 * makeDir方法用于在服务器端创建目录
	 * 
	 * @param dirName
	 *            String 目录名字
	 * @throws CardException
	 */
	public static void makeDir(String dirName) throws CardException {
		File dir = new File(dirName);
		if (dir.exists() && dir.isDirectory()) {
			return;
		}
		if (!dir.mkdir()) {
			throw new CardException(StringUtils.join(new String[] { "目录",
					dirName, "创建失败！" }));
		}
	}

	/**
	 * renameDir方法用于将服务器上的源目录名修改为目标目录名
	 * 
	 * @param sourceDirName
	 *            String 源目录名
	 * @param targetDirName
	 *            String 目标目录名
	 * @throws CardException
	 */
	public static void renameDir(String sourceDirName, String targetDirName)
			throws CardException {
		File sDir = new File(sourceDirName);
		File tDir = new File(targetDirName);
		if (sDir.exists() && sDir.isDirectory() && !tDir.exists()) {
			if (!sDir.renameTo(tDir)) {
				throw new CardException(StringUtils.join(new String[] { "目录",
						sourceDirName, "改名为", targetDirName, "时出现错误！" }));
			}
		}
	}

	/**
	 * deleteDir方法用于删除指定目录
	 * 
	 * @param dirName
	 *            String 要删除的目录名
	 * @throws CardException
	 */
	public static void deleteDir(String dirName) throws CardException {
		File dir = new File(dirName);
		deleteDirFiles(dir);
		// 删除目录
		if (!dir.delete()) {
			throw new CardException(StringUtils.join(new String[] { "目录",
					dirName, "删除失败！" }));
		}
	}

	public static void deleteDirFiles(File dir) throws CardException {
		if (dir.exists() && dir.isDirectory()) {
			// 检查目录是否为空，如果不为空则删除目录下的所有文件
			File[] files = dir.listFiles();
			if (null != files && files.length > 0) {
				for (File f : files) {
					if (!f.delete()) {
						throw new CardException(StringUtils.join(new String[] {
								"删除", dir.getName(), "目录下的", f.getName(),
								"文件时出现错误，目录删除失败！" }));
					}
				}
			}
		}
	}

	/**
	 * deleteFile方法用于删除指定路径下的的文件
	 * 
	 * @param path
	 *            String 路径名
	 * @param filename
	 *            String 文件名
	 * @throws CardException
	 */
	public static void deleteFile(String realPathFileName) {
		File file = new File(realPathFileName);
		if (file.exists() && file.isFile()) {
			file.delete();
			/*
			 * if (!file.delete()) { throw new
			 * CardException(StringUtils.join(new String[] { "删除",
			 * realPathFileName, "时出错！" })); }
			 */
		}
	}

	/**
	 * saveFile方法用于保存文件内容
	 * 
	 * @param path
	 *            String 保存文件的绝对路径
	 * @param filename
	 *            String 要保存的文件名
	 * @param content
	 *            String 保存的文件内容
	 * @throws IOException
	 * @throws CardException
	 */
	public static void saveFile(String realPathFileName, String content)
			throws IOException{		
		PrintWriter writer = null;		
		try {
			writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
					realPathFileName),"UTF-8"), true);			
			writer.write(content);			
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}
	
	/**
	 * saveFile方法用于保存文件内容
	 * 
	 * @param path
	 *            String 保存文件的绝对路径
	 * @param filename
	 *            String 要保存的文件名
	 * @param content
	 *            String 保存的文件内容
	 *  @param charSet 保存文件所用的字符集 默认为 utf-8
	 * @throws IOException
	 * @throws CardException
	 */
	public static void saveFile1(String realPathFileName, String content,String charSet)
			throws IOException{		
		PrintWriter writer = null;
		//默认为utf-f
		if(null==charSet || "".equals(charSet.trim())){
			charSet="utf-8";
		}
		try {
			writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(
					realPathFileName),charSet), true);			
			writer.write(content);			
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	/**
	 * readFromFile方法用于从指定文件中读取内容
	 * 
	 * @param realPathFileName
	 *            文件的物理路径
	 * @return String 文件内容
	 * @throws IOException
	 */
	public static String readFromFile(String realPathFileName)
			throws IOException {
		StringBuffer strBufFileContent = new StringBuffer();
		File file = new File(realPathFileName);
		if (file.exists() && file.isFile()) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file),"UTF-8"));
			String strLine = null;
			while ((strLine = reader.readLine()) != null) {
				strBufFileContent.append(strLine);
			}
		}
		return strBufFileContent.toString();
	}
	
	/**
	 * copyFiles用于将置顶文件拷贝至目标目录
	 * 
	 * @param sourceFileName
	 * @param targetFileName
	 * @throws IOException
	 * @throws CardException
	 */
	public static void copyFiles(String sourceFileName, String targetFileName)
			throws IOException, CardException{		
		FileUtils.saveFile(targetFileName, FileUtils.readFromFile(sourceFileName));
	}
}
